/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"

#include "UI/ui绘制模板.h"
#include "UI/实例部件/ui弹出菜单.h"
#include "UI/实例部件/ui菜单.h"

#include "面/Surface.h"



static vec2 on_弹出菜单项布局(C_弹出面板* box, C_Widget** w, uint32 num) {
	uint16 Num = box->m_项数据.size();
	if (!Num) return {128, 30};

	auto& font = f_surface_get字体库(*box->m_UIctx, S_UI主题::ui_默认字体);
	auto e = box->f_get更新部件();

	vec2 size = { 256, 256 };

	float32 边宽度 = 10;


	uint16 offsetID = 0;

	size.x = DEF_Max(Num * 100, 256);
	float32 maxH = 20;
	float32 项宽度 = (size.x - (边宽度 * Num + 边宽度)) / Num;

	//e.m_W[0]->f_setPos({ (size.x - 搜索框宽度)*0.5f, 5 });
	//e.m_W[0]->f_setSize({ 搜索框宽度, 20 });


	vec2 开始偏移 = { 边宽度, 0 };

	switch (box->m_方向) {
		case E_方向::e_横向: {
			for (uint16 i = 0; i < Num; ++i) {
				uint16 jNum = box->m_项数据[i].size();
				for (uint16 j = 0; j < jNum; ++j) {

					box->m_项数据[i][j].m_item[0];
					box->m_项数据[i][j].m_ID_X = offsetID;


					e.m_W[offsetID]->f_setPos({ 10,30 });

					++offsetID;
				}

				if (maxH < jNum * 20) {
					maxH = jNum * 20;
				}
			}
			break;
		}
		case E_方向::e_纵向: {
			for (uint16 i = 0; i < Num; ++i) {
				uint16 jNum = box->m_项数据[i].size();

				for (uint16 j = 0; j < jNum; ++j) {

					box->m_项数据[i][j].m_item[0];
					box->m_项数据[i][j].m_ID_X = offsetID;


					vec2 pos = 开始偏移 + vec2{ i * (项宽度 + 边宽度), j * 20.0f };

					e.m_W[offsetID]->f_setPos(pos);
					e.m_W[offsetID]->f_setSize({ 项宽度, 18.0f });

					++offsetID;
				}

				if (maxH < jNum * 20) {
					maxH = jNum * 20;
				}
			}
			break;
		}
	}


	size.y = maxH;

	return size;
}

static void on_弹出面板布局(C_Widget* self, C_Widget** w, uint32 num) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self);

	vec2 size = on_弹出菜单项布局(box, w, num);
	
	if (!self->m_父部件) {
		self->f_setSize(size);
		f_widget_单个部件裁剪(self,
						{ self->m_Pos.x, self->m_Pos.y, self->m_Pos.x + size.x, self->m_Pos.y + size.y },
						{ 0,0 }
		);
	}
}

static void on_弹出菜单Draw(C_Widget* self, S_2D画布* 画布) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self);
	box->m_背景绘制.m_属性 = f_vg_gen属性(画布);

	S_凸边图元 图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);

	box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);

	auto 字体 = f_surface_get字体(*self->m_UIctx, S_UI主题::ui_默认字体);
	box->m_字符绘制.f_Draw(画布, &字体, box->m_背景绘制.m_属性);

	box->m_图标绘制.f_Draw单色(画布, box->m_背景绘制.m_属性, S_UI主题::ui_默认图标);
}

static void on_弹出菜单变换修改(C_Widget* self) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self);


	mat3X2 Mat;
	Mat.row0 = {};
	Mat.row1 = box->m_Size;
	Mat.row2 = { S_UI主题::ui_面板圆角半径*2, 0 };

	box->m_背景绘制.f_alloc(1);
	box->m_背景绘制.f_set单色填充(0, Mat, S_UI主题::uic_深色 - S_RGBA8UI{ 10,10,10,0 }, self->m_图元);
	//Mat.row0.y = 24;
	//Mat.row1.y -= 24;
	//box->m_背景绘制.f_set单色填充(1, Mat, S_UI主题::uic_深色, S_Widget预设图元::m_圆角矩形);
	


	box->m_字符绘制.f_alloc(box->m_OffsetID, 12);
	uint16 offsetID = 0;
	auto e = box->f_get更新部件();

	uint16 Num = box->m_项数据.size();
	for (uint16 i = 0; i < Num; ++i) {
		uint16 jNum = box->m_项数据[i].size();
		for (uint16 j = 0; j < jNum; ++j) {
			

			Mat.row0 = e.m_W[offsetID]->m_Pos;
			Mat.row1 = e.m_W[offsetID]->m_Size;
			
			auto& str = box->m_项数据[i][j].m_item[0];
			box->m_字符绘制.f_绘制文本({ Mat.row0 , Mat.row1 }, S_UI主题::uic_文字颜色, offsetID, str, E_对齐方式::e_据中对齐);
			

			//box->m_图标绘制.f_Begin(1);
			//box->m_图标绘制.f_set(box->m_ico, 0, Mat, S_UI主题::uic_文字颜色);
			//box->m_图标绘制.f_End();

			++offsetID;
		}
	}

}


static bool on_弹出菜单项单击(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_弹出面板* but = static_cast<C_弹出面板*>(self);
	
	return true;
}

static bool on_弹出面板鼠标进入(C_Widget* self, const S_鼠标& 轮值) {
	C_Widget::g_是否打开了弹出窗口 = false;
	f_widget_取消定时关闭弹出组件();
	return false;
}

static bool on_弹出面板鼠标移出(C_Widget* self, const S_鼠标& 轮值) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self);
	f_widget_线程定时关闭弹出组件();
	return false;
}



static bool on_弹出面板项鼠标进入(C_Widget* self, const S_鼠标& 轮值) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self->m_父部件);
	mat3X2 Mat;
	Mat.row0 = self->m_Pos;
	Mat.row1 = self->m_Size;
	Mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };

	box->m_背景绘制.f_alloc(3);
	box->m_背景绘制.f_set单色填充(2, Mat, S_UI主题::uic_菜单焦点色, S_Widget预设图元::m_圆角矩形);
	return false;
}

static bool on_弹出面板项鼠标移出(C_Widget* self, const S_鼠标& 轮值) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self->m_父部件);
	box->m_背景绘制.f_Close(2);
	return true;
}




C_弹出面板::C_弹出面板(S_UI创建环境& ctx) : C_Widget(&ctx) {
	//mf_布局Fun = on_弹出面板布局;
	mf_DrawFun = on_弹出菜单Draw;
	mf_变换修改 = on_弹出菜单变换修改;


	mf_鼠标进入 = on_弹出面板鼠标进入;
	mf_鼠标离开 = on_弹出面板鼠标移出;

	m_lineBox = nullptr;
	m_图元 = S_Widget预设图元::m_圆角矩形;
	mf_项选中回调 = nullptr;

}

C_弹出面板::~C_弹出面板() {
}


bool on_弹出面板项点击(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self->m_父部件);
	C_项面板项* item = static_cast<C_项面板项*>(self);

	f_widget_顶层窗口_pop(0);
	if (box->mf_项选中回调) {
		//f_prop_映射引用2D(box->m_Value);
		box->mf_项选中回调(self, item->m_项);
	}
	return true;
}


C_Widget* f_ui_创建弹出面板(S_UI创建环境& ctx, E_方向 方向, bool 搜索框) {
	C_弹出面板* box = new C_弹出面板(ctx);

	box->m_方向 = 方向;
	//C_行编辑框* lineBox = new C_行编辑框(ctx);
	//box->f_添加子组件(lineBox);

	return box;
}

void f_ui_添加弹出面板菜单项(C_Widget* self, const S_列表填充数据& item, uvec2 组) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self);

	if (box->m_方向 == E_方向::e_横向) {

	}
	else {
		
	}
	if (组.x >= box->m_项数据.size()) {
		box->m_项数据.push_back({ item });
	}
	else {
		if (组.y <= box->m_项数据[组.x].size()) {
			auto& 相同str = item.m_item[0];
			uint32 num = box->m_项数据[组.x].size();
			for (auto& e : box->m_项数据[组.x]) {
				if (e.m_item[0] == 相同str) return;
			}
			box->m_项数据[组.x].insert(box->m_项数据[组.x].begin() + 组.y, item);
		}
		else {
			box->m_项数据[组.x].push_back(item);
		}
	}



	++box->m_OffsetID;
	C_项面板项* butItem = (C_项面板项*)box->f_get子组件(box->m_OffsetID);
	if (butItem == nullptr) {
		butItem = new C_项面板项;
		box->f_添加子组件(butItem, box->m_OffsetID);
	}
	
	butItem->mf_鼠标进入 = on_弹出面板项鼠标进入;
	butItem->mf_鼠标离开 = on_弹出面板项鼠标移出;
	butItem->mf_鼠标按键点击 = on_弹出面板项点击;
	butItem->m_项 = item;
	butItem->m_项.m_ID_X = 组.x;
	butItem->m_项.m_ID_Y = 组.y;
}

void f_ui_删除弹出面板菜单项(C_Widget* self, ivec2 id) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self);

	if (id.x >= 0) {
		--box->m_OffsetID;
	}
	else if (id.y >= 0) {
		--box->m_OffsetID;
	}
	else {
		box->m_项数据.clear();
		box->m_OffsetID = 0;
	}
}

void f_ui_弹出面板项回调绑定(C_Widget* self, fp_列表项选中回调 fn) {
	C_弹出面板* box = static_cast<C_弹出面板*>(self);
	box->mf_项选中回调 = fn;
}











static void on_布局超级弹出菜单(C_Widget* self, C_Widget** w, uint32 num) {
	C_超级弹出菜单* box = static_cast<C_超级弹出菜单*>(self);
	

	auto e = box->m_弹出面板->f_get更新部件();
	vec2 size = on_弹出菜单项布局(box->m_弹出面板, e.m_W, e.m_Num);
	box->m_弹出面板->f_setPos({4, 26});
	box->m_弹出面板->f_setSize(size);

	size.x += 8;
	size.y += 30;


	float32 搜索框最小宽度 = 160;
	box->m_搜索编辑框->f_setSize({ DEF_Max(size.x * 0.5f, 搜索框最小宽度), 22 });
	box->m_搜索编辑框->f_setPos({(size.x - box->m_搜索编辑框->m_Size.x) * 0.5f, 6});

	//box->m_滚动框->f_setPos({ 5, 30 });
	//box->m_滚动框->f_setSize({ size.x - 10, size.y - 30 });
	//box->f_setPos({ 5, 30 });
	
	if (!self->m_父部件) {
		self->f_setSize(size);
		f_widget_单个部件裁剪(self,
						{ self->m_Pos.x, self->m_Pos.y, self->m_Pos.x + size.x, self->m_Pos.y + size.y },
						{ 0,0 }
		);
	}

}

static void on_绘制超级弹出菜单(C_Widget* self, S_2D画布* 画布) {
	C_超级弹出菜单* box = static_cast<C_超级弹出菜单*>(self);
	
	box->m_背景绘制.m_属性 = f_vg_gen属性(画布);
	S_凸边图元 图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);

	box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);
	//box->m_文字绘制.f_Draw文本(画布, self->m_UIctx->m_文字字号, gDraw列表框_属性ID);
	box->m_图标绘制.f_Draw单色(画布, box->m_背景绘制.m_属性, S_UI主题::ui_默认图标);


}

static void on_变换超级弹出菜单(C_Widget* self) {
	C_超级弹出菜单* box = static_cast<C_超级弹出菜单*>(self);

	box->m_背景绘制.f_alloc(2);
	mat3X2 mat{};
	mat.row1 = self->m_Size;
	mat.row2 = {S_UI主题::ui_面板圆角半径*2, 0};
	box->m_背景绘制.f_set单色填充(0, mat, S_UI主题::uic_深色 - S_RGBA8UI{ 5,5,5,0 }, S_Widget预设图元::m_圆角矩形);
	mat.row0 = {2,2};
	mat.row1 -= 5;
	box->m_背景绘制.f_set单色填充(1, mat, S_UI主题::uic_深色, S_Widget预设图元::m_圆角矩形);
}

static bool on_超级弹出菜单鼠标进入(C_Widget* self, const S_鼠标& 鼠标) {
	//self->m_颜色A = self->m_颜色A + S_RGBA8UI{ 50, 50, 50, 127 };
	//self->m_父部件->m_绘制属性更新 = true;
	f_widget_取消定时关闭弹出组件();
	return false;
}

static bool on_超级弹出菜单鼠标退出(C_Widget* self, const S_鼠标& 鼠标) {
	//self->m_颜色A = self->m_颜色B;
	//self->m_父部件->m_绘制属性更新 = true;
	f_widget_线程定时关闭弹出组件(500);
	return false;
}

C_超级弹出菜单::C_超级弹出菜单(S_UI创建环境& ctx) : C_Widget(&ctx) {
	m_弹出面板 = new C_弹出面板(ctx);
	m_弹出面板->mf_鼠标进入 = nullptr;
	m_弹出面板->mf_鼠标离开 = nullptr;


	//m_滚动框 = new C_滚动框(ctx, false, true, m_弹出面板);
	m_搜索编辑框 = new C_行编辑框(ctx);


	f_添加子组件(m_搜索编辑框);
	//f_添加子组件(m_滚动框);
	f_添加子组件(m_弹出面板);
	

	mf_DrawFun = on_绘制超级弹出菜单;
	mf_布局Fun = on_布局超级弹出菜单;
	mf_变换修改 = on_变换超级弹出菜单;

	mf_鼠标进入 = on_超级弹出菜单鼠标进入;
	mf_鼠标离开 = on_超级弹出菜单鼠标退出;
}

C_超级弹出菜单::~C_超级弹出菜单() {
}

C_Widget* f_ui_创建扩展弹出菜单(S_UI创建环境& ctx, E_方向 布局方向, bool 搜索框) {
	C_超级弹出菜单* self = new C_超级弹出菜单(ctx);

	return self;
}

void f_ui_添加扩展弹出菜单项(C_Widget* self, const S_列表填充数据& item, const uvec2& 组) {
	C_超级弹出菜单* box = static_cast<C_超级弹出菜单*>(self);
	f_ui_添加弹出面板菜单项(box->m_弹出面板, item, 组);
}

void f_ui_删除扩展弹出菜单项(C_Widget* self, const ivec2& id) {
	C_超级弹出菜单* box = static_cast<C_超级弹出菜单*>(self);
	f_ui_删除弹出面板菜单项(box->m_弹出面板, id);
}

void f_ui_绑定扩展弹出菜单项回调绑定(C_Widget* self, fp_列表项选中回调 fn) {
	C_超级弹出菜单* box = static_cast<C_超级弹出菜单*>(self);
	f_ui_弹出面板项回调绑定(box->m_弹出面板, fn);
}




